// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Grab Bar Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.grabDelay

// States
private alias 0 : BAR_INACTIVE
private alias 1 : BAR_APPEARING
private alias 2 : BAR_MOVING
private alias 3 : BAR_DISAPPEARING

// Player Aliases
private alias object.state : player.state
private alias object.ypos : player.ypos
private alias object.xpos : player.xpos
private alias object.xvel : player.xvel
private alias object.yvel : player.yvel
private alias object.speed : player.speed
private alias object.gravity : player.gravity
private alias object.animation : player.animation
private alias object.collisionTop : player.collisionTop
private alias object.jumpPress : player.jumpPress


// ========================
// Events
// ========================

event ObjectUpdate
	if object.grabDelay > 0
		object.grabDelay--
	end if


	case ObjectUpdate
		object.xpos += object.xvel

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if player[currentPlayer].state != Player_State_Static
			if object.grabDelay == 0
				temp0 = player[currentPlayer].collisionTop

				BoxCollisionTest(C_TOUCH, object.entityPos, -24, 0, 24, 8, currentPlayer, -1, temp0, 1, temp0)
				if checkResult == true
					if player[currentPlayer].gravity == 1
						player[currentPlayer].animation = ANI_HANGING
						player[currentPlayer].speed 	= 0
						object.xvel = 2
						player[currentPlayer].yvel 		= 0
						player[currentPlayer].xvel 		= 0
						player[currentPlayer].xpos 		= player[currentPlayer].collisionTop
						player[currentPlayer].xpos++
						FlipSign(player[currentPlayer].xpos)
						player[currentPlayer].xpos <<= 16
						player[currentPlayer].xpos += object.xpos
					end if
				end if
			end if
break
			if player[currentPlayer].jumpPress == true
#platform: USE_STANDALONE
				player[currentPlayer].state = Player_State_Air
#endplatform
#platform: USE_ORIGINS
				player[currentPlayer].state = Player_State_Air_NoDropDash
#endplatform
				player[currentPlayer].xvel 		= -0x40000
				player[currentPlayer].gravity 	= 1
				object.grabDelay = 8
			end if

			if player[currentPlayer].state == Player_State_Static
				player[currentPlayer].xpos = player[currentPlayer].collisionTop
				player[currentPlayer].xpos++
				FlipSign(player[currentPlayer].xpos)
				player[currentPlayer].xpos <<= 16
				player[currentPlayer].xpos += object.xpos
			end if
		end if
	next
end event


event ObjectDraw
	if object.state > BAR_MOVING
		DrawSprite(object.frame)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("SCZ/Objects.gif")
	SpriteFrame(-24, -4, 48, 8, 80, 177)
	SpriteFrame(-24, -3, 48, 6, 80, 186)
	SpriteFrame(-24, -2, 48, 4, 80, 193)
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
